package b1.scenes.home
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	import b1.scenes.tower.*;
	import b1.utils.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.external.*;
	import flash.filters.*;
	import flash.net.*;
	import flash.text.*;
	import flash.ui.Mouse;

	public class FriendItem extends Sprite
	{
		private var playerInfo:PlayerInfo;
		private	const animalPosY:int = 80;
		private var friendItemTip:Sprite;
		
		public function FriendItem(playerInfo:PlayerInfo)
		{
			super();
			this.playerInfo = playerInfo;
			//背景框
			var TabItemBackground:Class = ResourceHelper.getInstance().getClassByName("TabItemBackground");
			var tabItemBackground:Sprite = new TabItemBackground(); 
			tabItemBackground.width = 110;
			tabItemBackground.height = 110;
			addChild(tabItemBackground);
			var titleText:TextField = new TextField();
			titleText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.LEFT;
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			//家背景
			tabItemBackground = new TabItemBackground();
			tabItemBackground.width = 110;
			tabItemBackground.height = 110;
			addChild(tabItemBackground);
			var HomeBackGround:Class;
			var homeBgrAlpha:Number;
			if (playerInfo == null)
			{
				var randHomeIndex:int = Math.random() * 2;
				HomeBackGround = ResourceHelper.getInstance().getClassByName("Home" + randHomeIndex);
				homeBgrAlpha = 0.5;
			}
			else
			{
				if (playerInfo.id < 0 && (playerInfo as VirtualPlayerConfig).homeIconImage != "")
				{
					HomeBackGround = ResourceHelper.getInstance().getClassByName((playerInfo as VirtualPlayerConfig).homeIconImage);
					homeBgrAlpha = 1;
				}
				else
				{
					HomeBackGround = ResourceHelper.getInstance().getClassByName("Home" + playerInfo.homeIndex);
					homeBgrAlpha = 0.5;
				}
			}
			var homeImage:Sprite = new HomeBackGround();
			homeImage.width = tabItemBackground.width;
			homeImage.height = tabItemBackground.height;
			homeImage.alpha = homeBgrAlpha;
			homeImage.mask = tabItemBackground;
			addChild(homeImage);
			if (playerInfo == null)
			{
				//标题名字
				titleText.text = "邀请好友";
				titleText.x = (tabItemBackground.width - titleText.width) / 2;
				titleText.y = (tabItemBackground.height - titleText.height) / 2;
				addChild(titleText);
			}
			else
			{
				//动物
				var animalMc:AnimalMc;
//				var animal:AnimalInfo;
				if (playerInfo.liveAnimals.length == 1)
				{
					animalMc = new AnimalMc(playerInfo.liveAnimals[0], 70);
					animalMc.x = this.width / 2;
					animalMc.y = animalPosY;
					addChild(animalMc);
				}
				else if (playerInfo.liveAnimals.length > 1)
				{
					for (var i:int = 0; i < 2; i++)
					{
						animalMc = new AnimalMc(playerInfo.liveAnimals[i], 35);
						animalMc.x = this.width * 5 / 16 + this.width * i * 6 / 16;
						animalMc.y = animalPosY;
						addChild(animalMc);
					}
				}
				//标题名字
				titleText.text = playerInfo.name.substr(0, 4);
				titleText.x = (tabItemBackground.width - titleText.width) / 2;
				titleText.y = 81;
				addChild(titleText);
			}
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			addEventListener(MouseEvent.CLICK, onFriendItemClick);
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			this.filters = [new ColorMatrixFilter(
				[1,0,0,0,15,
			     0,1,0,0,15,
			     0,0,1,0,15,
			     0,0,0,1,0 ])];
			if (playerInfo != null)
			{
				friendItemTip = new Sprite();
				var roundRec:RoundRec = new RoundRec(150, 160, 0.8, BFont.roundRecColor);
				friendItemTip.addChild(roundRec);
				
				//“单击跳转”
				var helpText:TextField = new TextField();
				helpText.selectable = false;
				helpText.width = roundRec.width;
				helpText.autoSize = TextFieldAutoSize.CENTER;
				var helpTexFormat:TextFormat = new TextFormat("微软雅黑", 15, 0x000000, true);
				helpTexFormat.leftMargin = 5;
				helpTexFormat.rightMargin = 5;
				helpTexFormat.color = 0x008000;
				helpText.defaultTextFormat = helpTexFormat;
				helpText.text = "单击跳转";
				helpText.y = 10;
				friendItemTip.addChild(helpText);
				
				var gap:int = 10;
				//名字
				var nameText:TextField = new TextField();
				nameText.selectable = false;
	        	nameText.width = roundRec.width;
	        	nameText.autoSize = TextFieldAutoSize.LEFT;
				var nameTexFormat:TextFormat = new TextFormat("微软雅黑", 15, 0x000000, true);
				nameTexFormat.leftMargin = 5;
				nameTexFormat.rightMargin = 5;
				nameText.defaultTextFormat = nameTexFormat;
				nameText.y = 30;
				nameText.x = gap;
				nameText.text = playerInfo.name.substr(0,4);
				friendItemTip.addChild(nameText);
				
				//传家宝
//				var heirloomsText:TextField = new TextField();
//				heirloomsText.selectable = false;
//				heirloomsText.autoSize = TextFieldAutoSize.LEFT;
//				var heirloomsTextFormat:TextFormat = new TextFormat("微软雅黑",13,0x000000);
//				heirloomsText.defaultTextFormat = heirloomsTextFormat;
//				var heirloomsNum:int  = 0;
//				for(var index:String in this.playerInfo.heirlooms)
//				{
//					heirloomsNum++;
//				}
//				heirloomsText.text = ("拥有传家宝: " + heirloomsNum);
//				heirloomsText.y = 50;
//				heirloomsText.x = gap;
//				friendItemTip.addChild(heirloomsText);
				
				//居住的动物
				var liveAnimalNames:TextField = new TextField();
				liveAnimalNames.selectable = false;
				liveAnimalNames.autoSize = TextFieldAutoSize.LEFT;
				var liveAnimalNamesTextFormat:TextFormat = new TextFormat("微软雅黑", 13, 0x000000);
				liveAnimalNames.defaultTextFormat = liveAnimalNamesTextFormat;
				liveAnimalNames.appendText( "居住动物: " + playerInfo.liveAnimals.length);
				liveAnimalNames.y = 50;
				liveAnimalNames.x = gap;
				friendItemTip.addChild(liveAnimalNames);
				var animalInfo:AnimalInfo = PlayerInfoHelper.getInstance().animals[this.playerInfo.liveAnimals[0]];
				var animalInfoText:TextField = new TextField();
				animalInfoText.selectable = false;
				animalInfoText.autoSize = TextFieldAutoSize.LEFT;
				animalInfoText.defaultTextFormat = liveAnimalNamesTextFormat;
				if (null != animalInfo)
				{
					animalInfoText.appendText("等级: " + animalInfo.level +"\n" + "血量: " + animalInfo.currentHealthPoint);
					animalInfoText.appendText("\n体力: " + animalInfo.currentVitalityPoint + "\n" + "速度: " + animalInfo.speed);
				}
				animalInfoText.y = 70;
				animalInfoText.x = gap;
				friendItemTip.addChild(animalInfoText);	
				
				friendItemTip.x = this.x + 100;
				friendItemTip.y = 475 + this.y - friendItemTip.height;
				var home:Home = SceneContainer.getInstance().currentScene as Home;
				home.addChild(friendItemTip);
			}
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			this.filters = null;
			if (playerInfo != null)
			{
				if (null != friendItemTip && null != friendItemTip.parent)
				{
					friendItemTip.parent.removeChild(friendItemTip);
				}
			}
		}
		
		private function onFriendItemClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			if (playerInfo == null)
			{
				ExternalInterface.call("sendRequest", true);
			}
			else
			{
				if (playerInfo is VirtualPlayerConfig && (playerInfo as VirtualPlayerConfig).towerConfig.towerLayerConfigs.length > 0)
				{
					var tower:Tower = Tower.getInstance();
					tower.loadConfig((playerInfo as VirtualPlayerConfig).towerConfig);
					tower.loadLayer(0);
					SceneContainer.getInstance().loadScene(tower);
				}
				else
				{
					var home:Home = SceneContainer.getInstance().currentScene as Home;
					home.switchLiveId(playerInfo.id);
				}
			}
		}

	}
}